<?php
/**
 * 广告位
 * User: cxm
 * Date: 2019/10/24
 * Time: 15:12
 */
namespace App\Http\Controllers\Admin\Adv;
use App\Http\Controllers\Admin\Controller;
use App\Models\Adv;
use App\Models\AdvPosition;
use DB,Validator;
use Illuminate\Http\Request;

class AdvPositionController extends Controller {
    /**
     * 广告位列表
     * URL GET:/admin/adv/position/get-lists
     * @param Request $request
     * @param string keyword 关键字
     * @param int page 页码
     * @param int limit 每页显示条数
     * @return mixed
     */
    public function getLists(Request $request){
        $params = $request->only(['keyword','page','limit']);
        if (!empty($params['keyword'])) {
            $params['where'][] = ['name','like','%'.$params['keyword'].'%'];
        }

        $params['orderBy'] = ['created_at desc','num asc'];
        $data = AdvPosition::listByWhere($params);
        return $this->success('查询成功',$data);
    }

    /**
     * 广告位添加、编辑处理
     * URL POST:/admin/adv/position/store
     * @param Request $request
     * @param int id 编辑时传
     * @param string name 广告位名称
     * @param int num 对应广告位数量
     * @param string width 广告位宽度 单位px
     * @param string height 广告位高度 单位px
     * @return mixed
     */
    public function store(Request $request){
        $params = $request->only(['id','name','num','width','height']);
        $id = isset($params['id']) ? $params['id'] : 0;
        $validator = Validator::make($params,[
            'name'          => ['required'],
            'num'           => ['required','integer','min:1']
        ],[
            'name.required'     => '广告位名称不能为空',
            'num.required'      => '请输入广告位对应广告数量',
            'num.integer'       => '广告数量错误,值为正整数',
            'num.min'           => '广告数量最小为1'
        ]);
        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        if ($id == 0) {
            $adv_position = new AdvPosition();
        }else{
            $adv_position = AdvPosition::find($id);
            if (!$adv_position) {
                return $this->error('广告位不存在');
            }
        }
        $adv_position->name         = $params['name'];
        $adv_position->num          = $params['num'];
        $adv_position->width        = isset($params['width']) ? $params['width'] : 0;
        $adv_position->height       = isset($params['height']) ? $params['height'] : 0;
        $result = $adv_position->save();
        if (!$result) {
            return $this->error('操作失败');
        }
        return $this->success('操作成功');
    }

    /**
     * 获取所有广告位的Id、名称
     * URL GET:/admin/adv/position/getIdAndName
     * @return mixed
     */
    public function getIdAndName(){
        $field = ['id','name'];
        $data = AdvPosition::getField($field);
        return $this->success('查询成功',$data);
    }

    /**
     * 删除广告位
     * URL POST:/admin/adv/position/delete
     * @param Request $request
     * @param array ids 广告位ID
     * @return mixed
     */
    public function delete(Request $request){
        $params = $request->only(['ids']);
        if (!array_key_exists('ids',$params) || empty($params['ids'])) {
            return $this->error('缺少广告位ID');
        }
        // 判断广告位是否有广告
        $isAdv = Adv::chooseIsAdv($params['ids']);
        if ($isAdv) {
            return $this->error('该广告位存在广告，请先删除广告');
        }
        $result = AdvPosition::whereIn('id',$params['ids'])->delete();
        if (!$result) {
            return $this->error('操作失败');
        }
        return $this->success('操作成功');
    }
}